package com.zuxia.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.catalina.core.ApplicationContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.hibernate.cfg.beanvalidation.ActivationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zuxia.entity.Depart;
import com.zuxia.entity.Posit;
import com.zuxia.entity.Staff;
import com.zuxia.service.IStaffService;


@Controller("staffAction")
@Scope("prototype")//定义单例或多例
@ParentPackage("struts-default")
@Namespace("/")
@Results({
	@Result(name="success",type="dispatcher",location="/admin/Staff_list.jsp"),
	@Result(name="yes",type="redirectAction",location="/selectstaff"),
	@Result(name="no",type="redirectAction",location="/selectstaff")
})
public class StaffAction extends ActionSupport implements ModelDriven<Staff>{

	private static final long serialVersionUID = 1L;
	
	private Staff staff =new Staff();
	
	private List<Staff> staffs;
	private List<Posit> posits;
	private int staffsSize;

	@Resource(name="staffService")
	private IStaffService staffService;
	
	public Staff getModel() {
		return staff;
	}
	
	@Action("addStaff")
	public String addStaff(){		
		return staffService.svaeStaff(staff);
	}
	@Action("selectstaff")
	public String selectStaff(){
		staffs =staffService.findAll();
		staffsSize=staffs.size();
		posits=staffService.findPosit();
		ActionContext.getContext().put("staffs", staffs);
		ActionContext.getContext().put("posits", posits);
		
		return "success";
	}
	
	@Action("updateStaff")
	public String updateStaff() {
		return staffService.updateStaff(staff);
	}
	@Action("deleteStaff")
	public String deleteStaff() {
		return staffService.deleteStaff(staff);
	}
	
	public List<Staff> getStaff() {
		return staffs;
	}
	
	public void setStaffs(List<Staff> staffs) {
		this.staffs = staffs;
	}
	
	public int getStaffsSize() {
		return staffsSize;
	}

	public void setStaffsSize(int staffsSize) {
		this.staffsSize = staffsSize;
	}
	
}
